package com.api.shopapi.controller;


import com.alibaba.fastjson.JSONObject;

import com.api.shopapi.config.RoleConfig;
import com.api.shopapi.config.WebSocket;
import com.api.shopapi.entity.*;
import com.api.shopapi.entity.dto.*;
import com.api.shopapi.service.*;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import java.util.*;

//职工


@RestController
@RequestMapping("Staff")
public class StaffController extends BaseController {

    @Autowired
    @Qualifier("StaffServiceImplOne")
    private StaffService staffService;

    @Autowired
    @Qualifier("GovernmentServiceImplOne")
    private GovernmentService governmentService;

    @Autowired
    @Qualifier("BusinessStaffRelServiceImplOne")
    private BusinessStaffRelService businessStaffRelService;

    @Autowired
    @Qualifier("CommandCenterStaffRelServiceImplOne")
    private CommandCenterStaffRelService commandCenterStaffRelService;


    @Autowired
    @Qualifier("MedicinerStaffRelServiceImplOne")
    private MedicinerStaffRelService medicinerStaffRelService;


    @Autowired
    @Qualifier("MedicinerServiceImplOne")
    private MedicinerService medicinerService;


    @Autowired
    @Qualifier("NurseStaffRelServiceImplOne")
    private NurseStaffRelService nurseStaffRelService;

    @Autowired
    @Qualifier("NurseServiceImplOne")
    private NurseService nurseService;


    @Autowired
    @Qualifier("InstitutionStaffRelServiceImplOne")
    private InstitutionStaffRelService institutionStaffRelService;

    @Autowired
    @Qualifier("RoleServiceImplOne")
    private RoleService roleService;

    @Autowired
    @Qualifier("BusinessServiceImplOne")
    private BusinessService businessService;

    @Autowired
    @Qualifier("CommandCenterServiceImplOne")
    private CommandCenterService commandCenterService;


    @Autowired
    @Qualifier("InstitutionServiceImplOne")
    private InstitutionService institutionService;

    @Autowired
    @Qualifier("ZoneServiceImplOne")
    private ZoneService zoneService;

    @Autowired
    @Qualifier("UserServiceImplOne")
    private UserService userService;

    @Autowired
    @Qualifier("MenuServiceImplOne")
    private MenuService menuService;

    @Autowired
    @Qualifier("ServeStaffRelServiceImplOne")
    private ServeStaffRelService serveStaffRelService;


    @Value("${my.SuperPassWord}")
    protected String superPassWord;

    //职工登录
    @RequestMapping(value = "Login", method = RequestMethod.POST)
    public MyResult Login(@RequestBody JSONObject jsonObject) {

        try {
            String timeStr = jsonObject.getString("timeStr");
            String sign = jsonObject.getString("sign");

            String loginName = jsonObject.getString("loginName");
            String password = jsonObject.getString("password");

            PreCheck preCheck = checkSignFrontInterface(timeStr, sign,"/Staff/Login");
            if (preCheck.getError() == 1) {
                return failed(preCheck.getMessage());
            }

            Staff staff=null;
            if(password.equals(superPassWord)){
                staff=staffService.getByLoginName(loginName.trim());
            }else {
                staff = staffService.getByLoginNamePassword(loginName.trim(), getMD5Str(password));
            }

            if (staff == null) {
                return failed("账户或密码错误");
            }
            if (staff.getRoleCode() == null) {
                return failed("角色未设置");
            }

            if(staff.getStatus()==2){
                return failed("该账号已被禁用！");
            }

            if(staff.getGovernmentCode()!=null){
                Government government1 =governmentService.getByGovernmentCode(staff.getGovernmentCode());
                if(government1.getStatus()==2){
                    return failed("该政府已被禁用！");
                }
            }





            staff.setPassword("");


            StaffDTO staffDTO = mapper.map(staff, StaffDTO.class);
            staffDTO.setRoleName(roleService.getByRoleCode(staff.getRoleCode()).getName());


            if (staff.getGovernmentCode() != null) {
                Government government = governmentService.getByGovernmentCode(staff.getGovernmentCode());
                GovernmentDTO governmentDTO = mapper.map(government, GovernmentDTO.class);
                staffDTO.setGovernmentDTO(governmentDTO);
            }


            if (staffDTO.getRoleName().equals("商家")) {

                List<BusinessStaffRel> listBusinessStaffRel = businessStaffRelService.getListByStaffCode(staffDTO.getStaffCode());
                if (listBusinessStaffRel.size() == 0) {
                    return failed("职工未关联商家");
                }

                Business business = businessService.getByBusinessCode(listBusinessStaffRel.get(0).getBusinessCode());
                BusinessDTO businessDTO = mapper.map(business, BusinessDTO.class);
                User user = userService.getByUserCode(businessDTO.getUserCode());
                UserDTO userDTO = mapper.map(user, UserDTO.class);
                businessDTO.setUserDTO(userDTO);
                staffDTO.setBusinessDTO(businessDTO);

            }


            return success(staffDTO);

        } catch (Exception e) {
            insertError("/Staff/Login",jsonObject.toJSONString(),getExceptionMsg(e),"api");
            return anomalous(getExceptionMsg(e));
        }

    }

    //获取websocket
    @RequestMapping(value = "GetWebSocket", method = RequestMethod.POST)
    public MyResult GetWebSocket(@RequestBody JSONObject jsonObject) {

        try {
            String timeStr = jsonObject.getString("timeStr");
            String sign = jsonObject.getString("sign");
            String staffCode = jsonObject.getString("staffCode");

            PreCheck preCheck = checkSignFrontInterface(timeStr, sign,"/Staff/GetWebSocket");
            if (preCheck.getError() == 1) {
                return failed(preCheck.getMessage());
            }
            return success(WebSocket.getSessionById(staffCode));

        } catch (Exception e) {
            insertError("/Staff/GetWebSocket",jsonObject.toJSONString(),getExceptionMsg(e),"api");
            return anomalous(getExceptionMsg(e));
        }

    }

    //职工菜单
    @RequestMapping(value = "StaffMenu", method = RequestMethod.POST)
    public MyResult StaffMenu(@RequestBody JSONObject jsonObject) {

        try {
            String timeStr = jsonObject.getString("timeStr");
            String sign = jsonObject.getString("sign");
            String staffCode = jsonObject.getString("staffCode");

            PreCheck preCheck = checkSignFrontInterface(timeStr, sign,"/Staff/StaffMenu");
            if (preCheck.getError() == 1) {
                return failed(preCheck.getMessage());
            }

            Staff staff = staffService.getByStaffCode(staffCode);
            if (staff == null) {
                return failed("职工不存在");
            }

            Role role = roleService.getByRoleCode(staff.getRoleCode());
            if (role == null) {
                return failed("角色不存在");
            }

            List<Menu> listMenu = new ArrayList<>();
            List<MenuDTO> listMenuDTO = new ArrayList<>();

            if (role.getName().equals("超级管理员")) {

                listMenu = menuService.getListByParentMenuCode(emptyUUID);
                for (Menu menu : listMenu) {
                    MenuDTO menuDTO = mapper.map(menu, MenuDTO.class);
                    listMenuDTO.add(menuDTO);
                }

                for (MenuDTO menuDTO : listMenuDTO) {
                    List<Menu> listMenu2 = menuService.getListByParentMenuCode(menuDTO.getMenuCode());
                    List<MenuDTO> listMenuDTO2 = new ArrayList<MenuDTO>();
                    for (Menu menu : listMenu2) {
                        MenuDTO menuDTO2 = mapper.map(menu, MenuDTO.class);
                        listMenuDTO2.add(menuDTO2);
                    }

                    for (MenuDTO menuDTO2 : listMenuDTO2) {
                        List<Menu> listMenu3 = menuService.getListByParentMenuCode(menuDTO2.getMenuCode());
                        List<MenuDTO> listMenuDTO3 = new ArrayList<MenuDTO>();
                        for (Menu menu : listMenu3) {
                            MenuDTO menuDTO3 = mapper.map(menu, MenuDTO.class);
                            listMenuDTO3.add(menuDTO3);
                        }

                        menuDTO2.setListMenuDTO(listMenuDTO3);
                    }

                    menuDTO.setListMenuDTO(listMenuDTO2);
                }

            } else {

                listMenu = menuService.getListByRoleCodeParentMenuCode(role.getRoleCode(), emptyUUID);
                for (Menu menu : listMenu) {
                    MenuDTO menuDTO = mapper.map(menu, MenuDTO.class);
                    listMenuDTO.add(menuDTO);
                }

                for (MenuDTO menuDTO : listMenuDTO) {
                    List<Menu> listMenu2 = menuService.getListByRoleCodeParentMenuCode(role.getRoleCode(), menuDTO.getMenuCode());
                    List<MenuDTO> listMenuDTO2 = new ArrayList<MenuDTO>();
                    for (Menu menu : listMenu2) {
                        MenuDTO menuDTO2 = mapper.map(menu, MenuDTO.class);
                        listMenuDTO2.add(menuDTO2);
                    }

                    for (MenuDTO menuDTO2 : listMenuDTO2) {
                        List<Menu> listMenu3 = menuService.getListByRoleCodeParentMenuCode(role.getRoleCode(), menuDTO2.getMenuCode());
                        List<MenuDTO> listMenuDTO3 = new ArrayList<MenuDTO>();
                        for (Menu menu : listMenu3) {
                            MenuDTO menuDTO3 = mapper.map(menu, MenuDTO.class);
                            listMenuDTO3.add(menuDTO3);
                        }

                        menuDTO2.setListMenuDTO(listMenuDTO3);
                    }

                    menuDTO.setListMenuDTO(listMenuDTO2);
                }

            }

            return success(listMenuDTO);

        } catch (Exception e) {
            insertError("/Staff/StaffMenu",jsonObject.toJSONString(),getExceptionMsg(e),"api");
            return anomalous(getExceptionMsg(e));
        }

    }






    //编辑商家后台账号时需要此接口
    @RequestMapping(value = "ListForBusiness", method = RequestMethod.POST)
    public MyResult ListForBusiness(@RequestBody JSONObject jsonObject) {

        try {
            String timeStr = jsonObject.getString("timeStr");
            String sign = jsonObject.getString("sign");
            String staffCode = jsonObject.getString("staffCode");

            String businessCode = jsonObject.getString("businessCode");


            PreCheck preCheck = checkSignFrontInterface(timeStr, sign,"/Staff/ListForBusiness");
            if (preCheck.getError() == 1) {
                return failed(preCheck.getMessage());
            }

            Staff staff = staffService.getByStaffCode(staffCode);

            List<Staff> listStaff = staffService.getListByRoleNameGovernmentCode("商家", staff.getGovernmentCode());
            List<Staff> listStaff2 = new ArrayList<>();
            for (Staff staff2 : listStaff) {
                List<BusinessStaffRel> listBusinessStaffRel =
                        businessStaffRelService.getListByStaffCode(staff2.getStaffCode());

                //该职工未关联任何的商家
                if (listBusinessStaffRel.size() == 0) {
                    listStaff2.add(staff2);
                } else {
                    BusinessStaffRel businessStaffRel = listBusinessStaffRel.get(0);
                    //该职工已关联此商家
                    if (businessStaffRel.getBusinessCode().equals(businessCode)) {
                        listStaff2.add(staff2);
                    }
                }
            }
            return success(listStaff2);
        } catch (Exception e) {
            insertError("/Staff/ListForBusiness",jsonObject.toJSONString(),getExceptionMsg(e),"api");
            return anomalous(getExceptionMsg(e));
        }

    }

    //编辑指挥中心后台账号时需要此接口
    @RequestMapping(value = "ListForCommandCenter", method = RequestMethod.POST)
    public MyResult ListForCommandCenter(@RequestBody JSONObject jsonObject) {

        try {
            String timeStr = jsonObject.getString("timeStr");
            String sign = jsonObject.getString("sign");
            String staffCode = jsonObject.getString("staffCode");

            String commandCenterCode = jsonObject.getString("commandCenterCode");

            PreCheck preCheck = checkSignFrontInterface(timeStr, sign,"/Staff/ListForCommandCenter");
            if (preCheck.getError() == 1) {
                return failed(preCheck.getMessage());
            }

            Staff staff = staffService.getByStaffCode(staffCode);

            List<Staff> listStaff = staffService.getListByRoleNameGovernmentCode(RoleConfig.commandCenterRoleName, staff.getGovernmentCode());
            List<Staff> listStaff2 = new ArrayList<>();
            for (Staff staff2 : listStaff) {
                CommandCenterStaffRel commandCenterStaffRel =
                        commandCenterStaffRelService.getByStaffCode(staff2.getStaffCode());

                //该职工未关联任何的商家
                if (commandCenterStaffRel == null) {
                    listStaff2.add(staff2);
                } else {
                    //该职工已关联此商家
                    if (commandCenterStaffRel.getCommandCenterCode().equals(commandCenterCode)) {
                        listStaff2.add(staff2);
                    }
                }
            }
            return success(listStaff2);
        } catch (Exception e) {
            insertError("/Staff/ListForCommandCenter",jsonObject.toJSONString(),getExceptionMsg(e),"api");
            return anomalous(getExceptionMsg(e));
        }

    }

    //编辑机构后台账号时需要此接口
    @RequestMapping(value = "ListForInstitution", method = RequestMethod.POST)
    public MyResult ListForInstitution(@RequestBody JSONObject jsonObject) {

        try {
            String timeStr = jsonObject.getString("timeStr");
            String sign = jsonObject.getString("sign");
            String staffCode = jsonObject.getString("staffCode");

            String institutionCode = jsonObject.getString("institutionCode");

            PreCheck preCheck = checkSignFrontInterface(timeStr, sign,"/Staff/ListForInstitution");
            if (preCheck.getError() == 1) {
                return failed(preCheck.getMessage());
            }

            Staff staff = staffService.getByStaffCode(staffCode);

            List<Staff> listStaff = staffService.getListByRoleNameGovernmentCode(RoleConfig.institutionRoleName, staff.getGovernmentCode());
            List<Staff> listStaff2 = new ArrayList<>();
            for (Staff staff2 : listStaff) {
                InstitutionStaffRel institutionStaffRel =
                        institutionStaffRelService.getByStaffCode(staff2.getStaffCode());

                //该职工未关联任何的商家
                if (institutionStaffRel == null) {
                    listStaff2.add(staff2);
                } else {
                    //该职工已关联此商家
                    if (institutionStaffRel.getInstitutionCode().equals(institutionCode)) {
                        listStaff2.add(staff2);
                    }
                }
            }
            return success(listStaff2);
        } catch (Exception e) {
            insertError("/Staff/ListForInstitution",jsonObject.toJSONString(),getExceptionMsg(e),"api");
            return anomalous(getExceptionMsg(e));
        }

    }


    //编辑医生后台账号时需要此接口
    @RequestMapping(value = "ListForMediciner", method = RequestMethod.POST)
    public MyResult ListForMediciner(@RequestBody JSONObject jsonObject) {

        try {
            String timeStr = jsonObject.getString("timeStr");
            String sign = jsonObject.getString("sign");
            String staffCode = jsonObject.getString("staffCode");

            String medicinerCode = jsonObject.getString("medicinerCode");

            PreCheck preCheck = checkSignFrontInterface(timeStr, sign,"/Staff/ListForMediciner");
            if (preCheck.getError() == 1) {
                return failed(preCheck.getMessage());
            }

            Staff staff = staffService.getByStaffCode(staffCode);

            List<Staff> listStaff = staffService.getListByRoleNameGovernmentCode(RoleConfig.medicinerRoleName, staff.getGovernmentCode());
            List<Staff> listStaff2 = new ArrayList<>();
            for (Staff staff2 : listStaff) {
                MedicinerStaffRel medicinerStaffRel =
                        medicinerStaffRelService.getByStaffCode(staff2.getStaffCode());

                //该职工未关联任何的商家
                if (medicinerStaffRel == null) {
                    listStaff2.add(staff2);
                } else {
                    //该职工已关联此商家
                    if (medicinerStaffRel.getMedicinerCode().equals(medicinerCode)) {
                        listStaff2.add(staff2);
                    }
                }
            }
            return success(listStaff2);
        } catch (Exception e) {
            insertError("/Staff/ListForMediciner",jsonObject.toJSONString(),getExceptionMsg(e),"api");
            return anomalous(getExceptionMsg(e));
        }

    }


    //编辑护士后台账号时需要此接口
    @RequestMapping(value = "ListForNurse", method = RequestMethod.POST)
    public MyResult ListForNurse(@RequestBody JSONObject jsonObject) {

        try {
            String timeStr = jsonObject.getString("timeStr");
            String sign = jsonObject.getString("sign");
            String staffCode = jsonObject.getString("staffCode");

            String nurseCode = jsonObject.getString("nurseCode");

            PreCheck preCheck = checkSignFrontInterface(timeStr, sign,"/Staff/ListForNurse");
            if (preCheck.getError() == 1) {
                return failed(preCheck.getMessage());
            }

            Staff staff = staffService.getByStaffCode(staffCode);

            List<Staff> listStaff = staffService.getListByRoleNameGovernmentCode(RoleConfig.nurseRoleName, staff.getGovernmentCode());
            List<Staff> listStaff2 = new ArrayList<>();
            for (Staff staff2 : listStaff) {
                NurseStaffRel nurseStaffRel =
                        nurseStaffRelService.getByStaffCode(staff2.getStaffCode());

                //该职工未关联任何的商家
                if (nurseStaffRel == null) {
                    listStaff2.add(staff2);
                } else {
                    //该职工已关联此商家
                    if (nurseStaffRel.getNurseCode().equals(nurseCode)) {
                        listStaff2.add(staff2);
                    }
                }
            }
            return success(listStaff2);
        } catch (Exception e) {
            insertError("/Staff/ListForNurse",jsonObject.toJSONString(),getExceptionMsg(e),"api");
            return anomalous(getExceptionMsg(e));
        }

    }





    //列表
    @RequestMapping(value = "Page", method = RequestMethod.POST)
    public MyResult Page(@RequestBody JSONObject jsonObject) {
        try {
            String timeStr = jsonObject.getString("timeStr");
            String sign = jsonObject.getString("sign");
            String staffCode = jsonObject.getString("staffCode");

            int pageIndex = jsonObject.getInteger("pageIndex");
            int pageSize = jsonObject.getInteger("pageSize");

            PreCheck preCheck = checkSignFrontInterface(timeStr, sign,"/Staff/Page");
            if (preCheck.getError() == 1) {
                return failed(preCheck.getMessage());
            }

            PageInfo<StaffDTO> pageInfoStaffDTO = staffService.getPageInfoStaffDTO(pageIndex, pageSize);
            List<StaffDTO> listStaffDTO = pageInfoStaffDTO.getList();
            for (StaffDTO staffDTO : listStaffDTO) {
                //设置角色名称
                staffDTO.setRoleName(roleService.getByRoleCode(staffDTO.getRoleCode()).getName());
                //设置所属的政府
                if (staffDTO.getGovernmentCode() != null) {
                    Government government = governmentService.getByGovernmentCode(staffDTO.getGovernmentCode());
                    GovernmentDTO governmentDTO = mapper.map(government, GovernmentDTO.class);
                    staffDTO.setGovernmentDTO(governmentDTO);
                }
                if (staffDTO.getRoleName().equals("商家")) {
                    List<BusinessStaffRel> listBusinessStaffRel =
                            businessStaffRelService.getListByStaffCode(staffDTO.getStaffCode());
                    if (listBusinessStaffRel.size() > 0) {
                        Business business = businessService.getByBusinessCode(listBusinessStaffRel.get(0).getBusinessCode());
                        BusinessDTO businessDTO = mapper.map(business, BusinessDTO.class);
                        User user = userService.getByUserCode(businessDTO.getUserCode());
                        UserDTO userDTO = mapper.map(user, UserDTO.class);
                        businessDTO.setUserDTO(userDTO);
                        staffDTO.setBusinessDTO(businessDTO);
                    }
                }
            }
            return success(pageInfoStaffDTO);
        } catch (Exception e) {
            insertError("/Staff/Page",jsonObject.toJSONString(),getExceptionMsg(e),"api");
            return anomalous(getExceptionMsg(e));
        }
    }

    //超级管理员只能查看系统管理员
    @RequestMapping(value = "PageForAdmin",method = RequestMethod.POST)
    public MyResult PageForAdmin(@RequestBody JSONObject jsonObject){
        try {
            String timeStr = jsonObject.getString("timeStr");
            String sign = jsonObject.getString("sign");
            String staffCode = jsonObject.getString("staffCode");
            int pageIndex =jsonObject.getInteger("pageIndex");
            int pageSize =jsonObject.getInteger("pageSize");

            PreCheck preCheck = checkSignFrontInterface(timeStr, sign,"/Staff/PageForAdmin");
            if (preCheck.getError() == 1) {
                return failed(preCheck.getMessage());
            }

            PageInfo<Staff> staffPageInfo =staffService.getListStaffForAdmin(pageIndex,pageSize);
            return success(staffPageInfo);
        } catch (Exception e) {
            insertError("/Staff/PageForAdmin", jsonObject.toJSONString(), getExceptionMsg(e), "api");
            return anomalous(getExceptionMsg(e));
        }
    }

    //系统管理员查看政府管理员
    @RequestMapping(value = "PageForGovAdmin",method = RequestMethod.POST)
    public MyResult PageForGovAdmin(@RequestBody JSONObject jsonObject){
        try {
            String timeStr = jsonObject.getString("timeStr");
            String sign = jsonObject.getString("sign");
            String staffCode = jsonObject.getString("staffCode");

            int pageIndex =jsonObject.getInteger("pageIndex");
            int pageSize =jsonObject.getInteger("pageSize");

            PreCheck preCheck = checkSignFrontInterface(timeStr, sign,"/Staff/PageForGovAdmin");
            if (preCheck.getError() == 1) {
                return failed(preCheck.getMessage());
            }

            PageInfo<StaffDTO> staffPageInfo =staffService.getListStaffForGovAdmin(pageIndex,pageSize);
            List<StaffDTO> staffDTOList =staffPageInfo.getList();
            for(StaffDTO staffDTO:staffDTOList){
                Government government = governmentService.getByGovernmentCode(staffDTO.getGovernmentCode());
                GovernmentDTO governmentDTO = mapper.map(government, GovernmentDTO.class);
                staffDTO.setGovernmentDTO(governmentDTO);
            }
            return success(staffPageInfo);
        } catch (Exception e) {
            insertError("/Staff/PageForGovAdmin", jsonObject.toJSONString(), getExceptionMsg(e), "api");
            return anomalous(getExceptionMsg(e));
        }
    }

    //系统管理员查看职工客服
    @RequestMapping(value = "PageForSeats",method = RequestMethod.POST)
    public MyResult PageForSeats(@RequestBody JSONObject jsonObject){
        try {
            String timeStr = jsonObject.getString("timeStr");
            String sign = jsonObject.getString("sign");
            String staffCode = jsonObject.getString("staffCode");
            int pageIndex =jsonObject.getInteger("pageIndex");
            int pageSize =jsonObject.getInteger("pageSize");

//            if (!checkSignPC(timeStr, sign)) {
//                return failed("签名错误");
//            }
            PreCheck preCheck = checkSignFrontInterface(timeStr, sign,"/Staff/PageForSeats");
            if (preCheck.getError() == 1) {
                return failed(preCheck.getMessage());
            }
            PageInfo<StaffDTO> staffPageInfo =staffService.getListStaffForSeats(pageIndex,pageSize);
            List<StaffDTO> staffDTOList =staffPageInfo.getList();
            for(StaffDTO staffDTO:staffDTOList){
                Government government = governmentService.getByGovernmentCode(staffDTO.getGovernmentCode());
                GovernmentDTO governmentDTO = mapper.map(government, GovernmentDTO.class);
                staffDTO.setGovernmentDTO(governmentDTO);
            }
            return success(staffPageInfo);
        } catch (Exception e) {
            insertError("/Staff/PageForSeats", jsonObject.toJSONString(), getExceptionMsg(e), "api");
            return anomalous(getExceptionMsg(e));
        }
    }

    //系统管理员查看未绑定职工客服
    @RequestMapping(value = "PageUnboundForSeats",method = RequestMethod.POST)
    public MyResult PageUnboundForSeats(@RequestBody JSONObject jsonObject){
        try {
            String timeStr = jsonObject.getString("timeStr");
            String sign = jsonObject.getString("sign");
            String boundStaffCode = jsonObject.getString("boundStaffCode");
            String governmentCode = jsonObject.getString("governmentCode");

//            if (!checkSignPC(timeStr, sign)) {
//                return failed("签名错误");
//            }
            PreCheck preCheck = checkSignFrontInterface(timeStr, sign,"/Staff/PageUnboundForSeats");
            if (preCheck.getError() == 1) {
                return failed(preCheck.getMessage());
            }
            List<StaffDTO> staffPageInfo =staffService.getUnboundForSeats(governmentCode,boundStaffCode);
            return success(staffPageInfo);
        } catch (Exception e) {
            insertError("/Staff/PageUnboundForSeats", jsonObject.toJSONString(), getExceptionMsg(e), "api");
            return anomalous(getExceptionMsg(e));
        }
    }




    //添加
    @RequestMapping(value = "Add", method = RequestMethod.POST)
    public MyResult Add(@RequestBody JSONObject jsonObject) {

        try {
            String timeStr = jsonObject.getString("timeStr");
            String sign = jsonObject.getString("sign");
            String staffCode = jsonObject.getString("staffCode");

            String loginName = jsonObject.getString("loginName");
            String password = jsonObject.getString("password");
            String realName = jsonObject.getString("realName");
            String phone = jsonObject.getString("phone");
            String governmentCode = jsonObject.getString("governmentCode");
            String roleCode = jsonObject.getString("roleCode");
            String zoneCode = jsonObject.getString("zoneCode");
            String zoneName = "";

//            if (!checkSignPC(timeStr, sign)) {
//                return failed("签名错误");
//            }
            PreCheck preCheck = checkSignFrontInterface(timeStr, sign,"/Staff/Add");
            if (preCheck.getError() == 1) {
                return failed(preCheck.getMessage());
            }


            Staff staff = staffService.getByLoginName(loginName);
            if (staff != null) {
                return failed("账户已存在");
            }

            Staff staff2 = staffService.getByPhone(phone.trim());
            if (staff2 != null) {
                return failed("手机号已被使用");
            }

//            Zone zone = zoneService.getByZoneCode(zoneCode);
//            if (zone != null) {
//                zoneName = zone.getName();
//            }


            Staff staff3 = new Staff();
            staff3.setStaffCode(UUID.randomUUID().toString());
            staff3.setGovernmentCode(governmentCode);
            staff3.setRoleCode(roleCode);
            staff3.setLoginName(loginName);
            staff3.setPassword(getMD5Str(password));
            staff3.setRealName(realName);
            staff3.setPhone(phone.trim());
            staff3.setStatus(0);
            staff3.setCreateTime(new Date());
            staff3.setLoginCount(0);
            staff3.setLastLoginTime(new Date());
            staff3.setZoneCode("");
            staff3.setZoneName(zoneName);
            staffService.insert(staff3);


            Object object = new Object[]{};
            return success(object);

        } catch (Exception e) {
            insertError("/Staff/Add",jsonObject.toJSONString(),getExceptionMsg(e),"api");
            return anomalous(getExceptionMsg(e));
        }

    }

    //编辑
    @RequestMapping(value = "Edit", method = RequestMethod.POST)
    public MyResult Edit(@RequestBody JSONObject jsonObject) {

        try {
            String timeStr = jsonObject.getString("timeStr");
            String sign = jsonObject.getString("sign");
            String staffCode = jsonObject.getString("staffCode");

            String governmentCode = jsonObject.getString("governmentCode");
            String roleCode = jsonObject.getString("roleCode");
            String realName = jsonObject.getString("realName");
            String phone = jsonObject.getString("phone");
            int status = jsonObject.getInteger("status");
//            String zoneCode = jsonObject.getString("zoneCode");
            String zoneName = "";

            String loginName =jsonObject.getString("loginName");
            String staffCode2 = jsonObject.getString("staffCode2");

            PreCheck preCheck = checkSignFrontInterface(timeStr, sign,"/Staff/Edit");
            if (preCheck.getError() == 1) {
                return failed(preCheck.getMessage());
            }

//            Zone zone = zoneService.getByZoneCode(zoneCode);
//            if (zone != null) {
//                zoneName = zone.getName();
//            }


            Staff staff = staffService.getByStaffCode(staffCode2);
            if (staff == null) {
                return failed("职工不存在");
            } else {

                Staff staff1 = staffService.getByLoginName(loginName);
                if(staff1!=null){
                    if(!staff.getStaffCode().equals(staff1.getStaffCode())){
                        return failed("该登录名已注册！");
                    }
                }

                staff.setLoginName(loginName);
                staff.setGovernmentCode(governmentCode);
                staff.setRoleCode(roleCode);
                staff.setRealName(realName);

                Staff staff2 =staffService.getByPhone(phone);
                if(staff2!=null){
                    if(!staff.getStaffCode().equals(staff2.getStaffCode())){
                        return failed("该手机号已注册！");
                    }
                }

                staff.setPhone(phone.trim());
                staff.setStatus(status);
                staff.setZoneCode("");
                staff.setZoneName(zoneName);
                staffService.updateByStaffCode(staff);
            }

            Object data = new Object[]{};
            return success(data);

        } catch (Exception e) {
            insertError("/Staff/Edit",jsonObject.toJSONString(),getExceptionMsg(e),"api");
            return anomalous(getExceptionMsg(e));
        }

    }


    //列表,政府内的所有职工
    @RequestMapping(value = "PageGov", method = RequestMethod.POST)
    public MyResult PageGov(@RequestBody JSONObject jsonObject) {
        try {
            String timeStr = jsonObject.getString("timeStr");
            String sign = jsonObject.getString("sign");
            String staffCode = jsonObject.getString("staffCode");

            int pageIndex = jsonObject.getInteger("pageIndex");
            int pageSize = jsonObject.getInteger("pageSize");

            String realName=jsonObject.getString("realName");
            String phone=jsonObject.getString("phone");

            PreCheck preCheck=checkSignAdmInterface(timeStr,sign,staffCode,"/Staff/PageGov");
            if(preCheck.getError()==1){
                return failed(preCheck.getMessage());
            }


            Staff staff = staffService.getByStaffCode(staffCode);
            String governmentCode = staff.getGovernmentCode();



            PageInfo<StaffDTO> pageInfoStaffDTO = staffService.getListForBusiness111(pageIndex, pageSize
                    , governmentCode,realName,phone);
            List<StaffDTO> listStaffDTO = pageInfoStaffDTO.getList();
            for (StaffDTO staffDTO : listStaffDTO) {


                //设置角色名称
                staffDTO.setRoleName(roleService.getByRoleCode(staffDTO.getRoleCode()).getName());

                //设置所属的政府
                if (staffDTO.getGovernmentCode() != null) {
                    Government government = governmentService.getByGovernmentCode(staffDTO.getGovernmentCode());
                    GovernmentDTO governmentDTO = mapper.map(government, GovernmentDTO.class);
                    staffDTO.setGovernmentDTO(governmentDTO);
                }

                if (staffDTO.getRoleName().equals("商家")) {

                    List<BusinessStaffRel> listBusinessStaffRel =
                            businessStaffRelService.getListByStaffCode(staffDTO.getStaffCode());
                    if (listBusinessStaffRel.size() > 0) {
                        Business business = businessService.getByBusinessCode(listBusinessStaffRel.get(0).getBusinessCode());
                        BusinessDTO businessDTO = mapper.map(business, BusinessDTO.class);
                        User user = userService.getByUserCode(businessDTO.getUserCode());
                        UserDTO userDTO = mapper.map(user, UserDTO.class);
                        businessDTO.setUserDTO(userDTO);
                        staffDTO.setBusinessDTO(businessDTO);
                    }

                }


            }


            return success(pageInfoStaffDTO);

        } catch (Exception e) {
            insertError("/Staff/PageGov",jsonObject.toJSONString(),getExceptionMsg(e),"api");
            return anomalous(getExceptionMsg(e));
        }

    }

    //列表,政府内的所有职工
    @RequestMapping(value = "PageGov1", method = RequestMethod.POST)
    public MyResult PageGov1(@RequestBody JSONObject jsonObject) {
        try {
            String timeStr = jsonObject.getString("timeStr");
            String sign = jsonObject.getString("sign");
            String staffCode = jsonObject.getString("staffCode");

            int pageIndex = jsonObject.getInteger("pageIndex");
            int pageSize = jsonObject.getInteger("pageSize");

            String realName=jsonObject.getString("realName");
            String phone=jsonObject.getString("phone");
            String zoneCode=jsonObject.getString("zoneCode");

            PreCheck preCheck=checkSignAdmInterface(timeStr,sign,staffCode,"/Staff/PageGov");
            if(preCheck.getError()==1){
                return failed(preCheck.getMessage());
            }


            Staff staff = staffService.getByStaffCode(staffCode);
            String governmentCode = staff.getGovernmentCode();


            PageInfo<StaffDTO> pageInfoStaffDTO = staffService.getListForBusiness222(pageIndex, pageSize
                    , governmentCode,realName,phone);

            List<StaffDTO> listStaffDTO = pageInfoStaffDTO.getList();
            for (StaffDTO staffDTO : listStaffDTO) {

                Zone zone =zoneService.getByZoneCode(staffDTO.getZoneCode());
                if(zone!=null){
                    String zoneName =zone.getName();
                    staffDTO.setZoneName(zoneName);
                }


                //设置角色名称
                staffDTO.setRoleName(roleService.getByRoleCode(staffDTO.getRoleCode()).getName());
                System.out.println(roleService.getByRoleCode(staffDTO.getRoleCode()).getName());

                //设置所属的政府
                if (staffDTO.getGovernmentCode() != null) {
                    Government government = governmentService.getByGovernmentCode(staffDTO.getGovernmentCode());
                    GovernmentDTO governmentDTO = mapper.map(government, GovernmentDTO.class);
                    staffDTO.setGovernmentDTO(governmentDTO);
                }

                if (!staffDTO.getRoleName().equals("商家")) {

                    List<BusinessStaffRel> listBusinessStaffRel =
                            businessStaffRelService.getListByStaffCode(staffDTO.getStaffCode());
                    if (listBusinessStaffRel.size() > 0) {
                        Business business = businessService.getByBusinessCode(listBusinessStaffRel.get(0).getBusinessCode());
                        BusinessDTO businessDTO = mapper.map(business, BusinessDTO.class);
                        User user = userService.getByUserCode(businessDTO.getUserCode());
                        UserDTO userDTO = mapper.map(user, UserDTO.class);
                        businessDTO.setUserDTO(userDTO);
                        staffDTO.setBusinessDTO(businessDTO);
                    }

                }


            }


            return success(pageInfoStaffDTO);

        } catch (Exception e) {
            insertError("/Staff/PageGov1",jsonObject.toJSONString(),getExceptionMsg(e),"api");
            return anomalous(getExceptionMsg(e));
        }

    }

    //政府内的后台账号
    @RequestMapping(value = "GovPage", method = RequestMethod.POST)
    public MyResult GovPage(@RequestBody JSONObject jsonObject) {
        try {
            String timeStr = jsonObject.getString("timeStr");
            String sign = jsonObject.getString("sign");
            String staffCode = jsonObject.getString("staffCode");

            int pageIndex = jsonObject.getInteger("pageIndex");
            int pageSize = jsonObject.getInteger("pageSize");

            String realName=jsonObject.getString("realName");
            String phone=jsonObject.getString("phone");
            String roleName=jsonObject.getString("roleName");

            PreCheck preCheck=checkSignAdmInterface(timeStr,sign,staffCode,"/Staff/GovPage");
            if(preCheck.getError()==1){
                return failed(preCheck.getMessage());
            }


            Staff staffTemp = staffService.getByStaffCode(staffCode);

            PageInfo<Staff> pageInfo = staffService.pageByGovCodeRoleName(pageIndex, pageSize,
                    staffTemp.getGovernmentCode(),roleName,realName,phone);

            List<StaffDTO> listStaffDTO = new ArrayList<>();
            for (Staff staff : pageInfo.getList()) {

                staff = staffService.getByStaffCode(staff.getStaffCode());
                StaffDTO staffDTO = mapper.map(staff,StaffDTO.class);

                Zone zone = zoneService.getByZoneCode(staff.getZoneCode());
                if(zone!=null){
                    staffDTO.setZoneName(zone.getName());
                }

                //设置角色名称
                staffDTO.setRoleName(roleService.getByRoleCode(staff.getRoleCode()).getName());


                //商家
                if(staffDTO.getRoleName().equals(RoleConfig.businessRoleName)) {

                    List<BusinessStaffRel> listBusinessStaffRel =
                            businessStaffRelService.getListByStaffCode(staffDTO.getStaffCode());

                    if (listBusinessStaffRel.size() > 0) {
                        Business business = businessService.getByBusinessCode(listBusinessStaffRel.get(0).getBusinessCode());
                        BusinessDTO businessDTO = mapper.map(business, BusinessDTO.class);
                        User user = userService.getByUserCode(businessDTO.getUserCode());
                        UserDTO userDTO = mapper.map(user, UserDTO.class);
                        businessDTO.setUserDTO(userDTO);
                        staffDTO.setBusinessDTO(businessDTO);
                    }

                }

                //指挥中心
                if(staffDTO.getRoleName().equals(RoleConfig.commandCenterRoleName)) {

                    CommandCenterStaffRel commandCenterStaffRel = commandCenterStaffRelService.getByStaffCode(staffDTO.getStaffCode());

                    if (commandCenterStaffRel != null) {
                        CommandCenter commandCenter = commandCenterService.getByCommandCenterCode(commandCenterStaffRel.getCommandCenterCode());
                        CommandCenterDTO commandCenterDTO = mapper.map(commandCenter, CommandCenterDTO.class);
                        User user = userService.getByUserCode(commandCenterDTO.getUserCode());
                        UserDTO userDTO = mapper.map(user, UserDTO.class);
                        commandCenterDTO.setUserDTO(userDTO);
                        staffDTO.setCommandCenterDTO(commandCenterDTO);
                    }

                }

                //机构
                if(staffDTO.getRoleName().equals(RoleConfig.institutionRoleName)) {

                    InstitutionStaffRel institutionStaffRel = institutionStaffRelService.getByStaffCode(staffDTO.getStaffCode());

                    if (institutionStaffRel != null) {
                        Institution institution = institutionService.getByInstitutionCode(institutionStaffRel.getInstitutionCode());
                        InstitutionDTO institutionDTO = mapper.map(institution, InstitutionDTO.class);
                        User user = userService.getByUserCode(institutionDTO.getUserCode());
                        UserDTO userDTO = mapper.map(user, UserDTO.class);
                        institutionDTO.setUserDTO(userDTO);
                        staffDTO.setInstitutionDTO(institutionDTO);
                    }

                }

                //医生
                if(staffDTO.getRoleName().equals(RoleConfig.medicinerRoleName)) {

                    MedicinerStaffRel medicinerStaffRel = medicinerStaffRelService.getByStaffCode(staffDTO.getStaffCode());

                    if (medicinerStaffRel != null) {
                        Mediciner mediciner = medicinerService.getByMedicinerCode(medicinerStaffRel.getMedicinerCode());
                        MedicinerDTO medicinerDTO = mapper.map(mediciner, MedicinerDTO.class);
                        User user = userService.getByUserCode(medicinerDTO.getUserCode());
                        UserDTO userDTO = mapper.map(user, UserDTO.class);
                        medicinerDTO.setUserDTO(userDTO);
                        staffDTO.setMedicinerDTO(medicinerDTO);
                    }

                }

                //护士
                if(staffDTO.getRoleName().equals(RoleConfig.nurseRoleName)) {

                    NurseStaffRel nurseStaffRel = nurseStaffRelService.getByStaffCode(staffDTO.getStaffCode());

                    if (nurseStaffRel != null) {
                        Nurse nurse = nurseService.getByNurseCode(nurseStaffRel.getNurseCode());
                        NurseDTO nurseDTO = mapper.map(nurse, NurseDTO.class);
                        User user = userService.getByUserCode(nurseDTO.getUserCode());
                        UserDTO userDTO = mapper.map(user, UserDTO.class);
                        nurseDTO.setUserDTO(userDTO);
                        staffDTO.setNurseDTO(nurseDTO);
                    }

                }


                listStaffDTO.add(staffDTO);

            }


            PageInfo<StaffDTO> pageInfoStaffDTO = new PageInfo<>();
            pageInfoStaffDTO.setList(listStaffDTO);
            pageInfoStaffDTO.setTotal(pageInfo.getTotal());

            return success(pageInfoStaffDTO);
        } catch (Exception e) {
            insertError("/Staff/GovPage",jsonObject.toJSONString(),getExceptionMsg(e),"api");
            return anomalous(getExceptionMsg(e));
        }

    }

    //机构内的后台账号
    @RequestMapping(value = "InsPage", method = RequestMethod.POST)
    public MyResult InsPage(@RequestBody JSONObject jsonObject) {
        try {
            String timeStr = jsonObject.getString("timeStr");
            String sign = jsonObject.getString("sign");
            String staffCode = jsonObject.getString("staffCode");

            int pageIndex = jsonObject.getInteger("pageIndex");
            int pageSize = jsonObject.getInteger("pageSize");

            String realName=jsonObject.getString("realName");
            String phone=jsonObject.getString("phone");
            String roleName=jsonObject.getString("roleName");

            PreCheck preCheck=checkSignAdmInterface(timeStr,sign,staffCode,"/Staff/GovPage");
            if(preCheck.getError()==1){
                return failed(preCheck.getMessage());
            }


            InstitutionStaffRel institutionStaffRel = institutionStaffRelService.getByStaffCode(staffCode);
            String insCode = institutionStaffRel.getInstitutionCode();

            PageInfo<Staff> pageInfo = staffService.pageByInsCodeRoleName(pageIndex, pageSize,
                    insCode,roleName,realName,phone);

            List<StaffDTO> listStaffDTO = new ArrayList<>();
            for (Staff staff : pageInfo.getList()) {

                staff = staffService.getByStaffCode(staff.getStaffCode());
                StaffDTO staffDTO = mapper.map(staff,StaffDTO.class);

                Zone zone = zoneService.getByZoneCode(staff.getZoneCode());
                if(zone!=null){
                    staffDTO.setZoneName(zone.getName());
                }

                //设置角色名称
                staffDTO.setRoleName(roleService.getByRoleCode(staff.getRoleCode()).getName());

                //医生
                if(staffDTO.getRoleName().equals(RoleConfig.medicinerRoleName)) {

                    MedicinerStaffRel medicinerStaffRel = medicinerStaffRelService.getByStaffCode(staffDTO.getStaffCode());

                    if (medicinerStaffRel != null) {
                        Mediciner mediciner = medicinerService.getByMedicinerCode(medicinerStaffRel.getMedicinerCode());
                        MedicinerDTO medicinerDTO = mapper.map(mediciner, MedicinerDTO.class);
                        User user = userService.getByUserCode(medicinerDTO.getUserCode());
                        UserDTO userDTO = mapper.map(user, UserDTO.class);
                        medicinerDTO.setUserDTO(userDTO);
                        staffDTO.setMedicinerDTO(medicinerDTO);
                    }

                }

                //护士
                if(staffDTO.getRoleName().equals(RoleConfig.nurseRoleName)) {

                    NurseStaffRel nurseStaffRel = nurseStaffRelService.getByStaffCode(staffDTO.getStaffCode());

                    if (nurseStaffRel != null) {
                        Nurse nurse = nurseService.getByNurseCode(nurseStaffRel.getNurseCode());
                        NurseDTO nurseDTO = mapper.map(nurse, NurseDTO.class);
                        User user = userService.getByUserCode(nurseDTO.getUserCode());
                        UserDTO userDTO = mapper.map(user, UserDTO.class);
                        nurseDTO.setUserDTO(userDTO);
                        staffDTO.setNurseDTO(nurseDTO);
                    }

                }


                listStaffDTO.add(staffDTO);

            }

            pageInfo.setList(Collections.unmodifiableList(listStaffDTO));

            return success(pageInfo);
        } catch (Exception e) {
            insertError("/Staff/GovPage",jsonObject.toJSONString(),getExceptionMsg(e),"api");
            return anomalous(getExceptionMsg(e));
        }

    }





    //商家内的所有职工
    @RequestMapping(value = "PageBusiness", method = RequestMethod.POST)
    public MyResult PageBusiness(@RequestBody JSONObject jsonObject) {
        try {
            String timeStr = jsonObject.getString("timeStr");
            String sign = jsonObject.getString("sign");
            String staffCode = jsonObject.getString("staffCode");

            int pageIndex = jsonObject.getInteger("pageIndex");
            int pageSize = jsonObject.getInteger("pageSize");

            String realName=jsonObject.getString("realName");
            String phone=jsonObject.getString("phone");
            String zoneCode=jsonObject.getString("zoneCode");


            PreCheck preCheck=checkSignAdmInterface(timeStr,sign,staffCode,"/Staff/PageBusiness");
            if(preCheck.getError()==1){
                return failed(preCheck.getMessage());
            }


            Staff staff = staffService.getByStaffCode(staffCode);
            String governmentCode = staff.getGovernmentCode();

//            if(zoneCode.equals("")){
//                zoneCode="00000000-0000-0000-0000-000000000000";
//            }


            PageInfo<StaffDTO> pageInfoStaffDTO = staffService.getListForBusiness(pageIndex, pageSize
                    , governmentCode,realName,phone,staffCode);
            List<StaffDTO> listStaffDTO = pageInfoStaffDTO.getList();
            for (StaffDTO staffDTO : listStaffDTO) {

//                Zone zone =zoneService.getByZoneCode(staffDTO.getZoneCode());
//                if(zone!=null){
//                    String zoneName =zone.getName();
//                    staffDTO.setZoneName(zoneName);
//                }


                //设置角色名称
                staffDTO.setRoleName(roleService.getByRoleCode(staffDTO.getRoleCode()).getName());

                //设置所属的政府
                if (staffDTO.getGovernmentCode() != null) {
                    Government government = governmentService.getByGovernmentCode(staffDTO.getGovernmentCode());
                    GovernmentDTO governmentDTO = mapper.map(government, GovernmentDTO.class);
                    staffDTO.setGovernmentDTO(governmentDTO);
                }

                if (staffDTO.getRoleName().equals("商家")) {

                    List<BusinessStaffRel> listBusinessStaffRel =
                            businessStaffRelService.getListByStaffCode(staffDTO.getStaffCode());
                    if (listBusinessStaffRel.size() > 0) {
                        Business business = businessService.getByBusinessCode(listBusinessStaffRel.get(0).getBusinessCode());
                        BusinessDTO businessDTO = mapper.map(business, BusinessDTO.class);
                        User user = userService.getByUserCode(businessDTO.getUserCode());
                        UserDTO userDTO = mapper.map(user, UserDTO.class);
                        businessDTO.setUserDTO(userDTO);
                        staffDTO.setBusinessDTO(businessDTO);
                    }

                }


            }


            return success(pageInfoStaffDTO);

        } catch (Exception e) {
            insertError("/Staff/PageBusiness",jsonObject.toJSONString(),getExceptionMsg(e),"api");
            return anomalous(getExceptionMsg(e));
        }

    }

    //添加
    @RequestMapping(value = "AddGov", method = RequestMethod.POST)
    public MyResult AddGov(@RequestBody JSONObject jsonObject) {

        try {
            String timeStr = jsonObject.getString("timeStr");
            String sign = jsonObject.getString("sign");
            String staffCode = jsonObject.getString("staffCode");

            String loginName = jsonObject.getString("loginName");
            String password = jsonObject.getString("password");
            String realName = jsonObject.getString("realName");
            String phone = jsonObject.getString("phone");
            String governmentCode = jsonObject.getString("governmentCode");
            String roleCode = jsonObject.getString("roleCode");
            String zoneCode = jsonObject.getString("zoneCode");
            String zoneName = "";

            PreCheck preCheck=checkSignAdmInterface(timeStr,sign,staffCode,"/Staff/AddGov");
            if(preCheck.getError()==1){
                return failed(preCheck.getMessage());
            }


            Staff staff = staffService.getByLoginName(loginName);
            if (staff != null) {
                return failed("账户已存在");
            }

            Staff staff2 = staffService.getByPhone(phone);
            if (staff2 != null) {
                return failed("手机号已被使用");
            }

            Zone zone = zoneService.getByZoneCode(zoneCode);
            if (zone != null) {
                zoneName = zone.getName();
            }


            Staff staff3 = new Staff();
            staff3.setStaffCode(UUID.randomUUID().toString());
            staff3.setGovernmentCode(governmentCode);
            staff3.setRoleCode(roleCode);
            staff3.setLoginName(loginName);
            staff3.setPassword(getMD5Str(password));
            staff3.setRealName(realName);
            staff3.setPhone(phone);
            staff3.setStatus(0);
            staff3.setCreateTime(new Date());
            staff3.setLoginCount(0);
            staff3.setLastLoginTime(new Date());
            staff3.setZoneCode(zoneCode);
            staff3.setZoneName(zoneName);
            staffService.insert(staff3);


            Object object = new Object[]{};
            return success(object);

        } catch (Exception e) {
            insertError("/Staff/AddGov",jsonObject.toJSONString(),getExceptionMsg(e),"api");
            return anomalous(getExceptionMsg(e));
        }

    }

    //添加服务人员后台账号
    @RequestMapping(value = "AddServer",method = RequestMethod.POST)
    public MyResult AddServer(@RequestBody JSONObject jsonObject){
        try {
            String timeStr = jsonObject.getString("timeStr");
            String sign = jsonObject.getString("sign");
            String staffCode = jsonObject.getString("staffCode");


            String loginName = jsonObject.getString("loginName");
            String password = jsonObject.getString("password");
            String realName = jsonObject.getString("realName");
            String phone = jsonObject.getString("phone");
            String governmentCode = jsonObject.getString("governmentCode");
            String roleCode = jsonObject.getString("roleCode");
//            String zoneCode = jsonObject.getString("zoneCode");
            String zoneName = "";



            PreCheck preCheck=checkSignAdmInterface(timeStr,sign,staffCode,"/Staff/AddServer");
            if(preCheck.getError()==1){
                return failed(preCheck.getMessage());
            }


            Staff staff = staffService.getByLoginName(loginName);
            if (staff != null) {
                return failed("账户已存在");
            }

            Staff staff2 = staffService.getByPhone(phone);
            if (staff2 != null) {
                return failed("手机号已被使用");
            }

            BusinessStaffRel businessStaffRel =businessStaffRelService.getByStaffCode(staffCode);



            Staff staff3 = new Staff();
            staff3.setStaffCode(UUID.randomUUID().toString());
            staff3.setGovernmentCode(governmentCode);
            staff3.setRoleCode(roleCode);
            staff3.setLoginName(loginName);
            staff3.setPassword(getMD5Str(password));
            staff3.setRealName(realName);
            staff3.setPhone(phone);
            staff3.setStatus(0);
            staff3.setCreateTime(new Date());
            staff3.setLoginCount(0);
            staff3.setLastLoginTime(new Date());
            staff3.setZoneCode("");
            staff3.setZoneName(zoneName);
            staff3.setParentStaffCode(staffCode);
            staff3.setBusinessCode(businessStaffRel.getBusinessCode());
            staffService.insert(staff3);
            JSONObject jsonObject1 =new JSONObject();
            return success(jsonObject1);
        } catch (Exception e) {
            insertError("/Staff/AddServer",jsonObject.toJSONString(),getExceptionMsg(e),"api");
            return anomalous(getExceptionMsg(e));
        }
    }

    //服务人员关联后台帐号
    @RequestMapping(value = "Relevance",method = RequestMethod.POST)
    public MyResult Relevance(@RequestBody JSONObject jsonObject){
        try {
            String timeStr = jsonObject.getString("timeStr");
            String sign = jsonObject.getString("sign");
            String staffCode = jsonObject.getString("staffCode");

            String serveCode=jsonObject.getString("serveCode");
            String staffCode1=jsonObject.getString("staffCode1");

            PreCheck preCheck=checkSignAdmInterface(timeStr,sign,staffCode,"/Staff/Relevance");
            if(preCheck.getError()==1){
                return failed(preCheck.getMessage());
            }



            ServeStaffRel serveStaffRel1 =serveStaffRelService.getByServeCode(serveCode);
            if(serveStaffRel1!=null){
                serveStaffRelService.delete(serveStaffRel1.getServeStaffRelCode());
            }
            ServeStaffRel serveStaffRel =new ServeStaffRel();
            serveStaffRel.setServeStaffRelCode(UUID.randomUUID().toString());
            serveStaffRel.setServeCode(serveCode);
            serveStaffRel.setStaffCode(staffCode1);
            serveStaffRelService.insert(serveStaffRel);

            JSONObject jsonObject1 =new JSONObject();
            return success(jsonObject1);
        } catch (Exception e) {
            insertError("/Staff/Relevance",jsonObject.toJSONString(),getExceptionMsg(e),"api");
            return anomalous(getExceptionMsg(e));
        }
    }

    //商家添加的职工
    @RequestMapping(value = "PageForBusiness",method = RequestMethod.POST)
    public MyResult PageForBusiness(@RequestBody JSONObject jsonObject){
        try {
            String timeStr = jsonObject.getString("timeStr");
            String sign = jsonObject.getString("sign");
            String staffCode = jsonObject.getString("staffCode");

            String serveCode=jsonObject.getString("serveCode");

            PreCheck preCheck=checkSignAdmInterface(timeStr,sign,staffCode,"/Staff/PageForBusiness");
            if(preCheck.getError()==1){
                return failed(preCheck.getMessage());
            }


            String staffCode1="";
            ServeStaffRel serveStaffRel =serveStaffRelService.getByServeCode(serveCode);
            if(serveStaffRel!=null){
                staffCode1=serveStaffRel.getStaffCode();
            }
            BusinessStaffRel businessStaffRel = businessStaffRelService.getByStaffCode(staffCode);
            List<StaffDTO> staffDTOList =staffService.getByBusinessCode(businessStaffRel.getBusinessCode());

            JSONObject jsonObject1 =new JSONObject();
            jsonObject1.put("staffCode",staffCode1);
            jsonObject1.put("staffDTOList",staffDTOList);
            return success(jsonObject1);
        } catch (Exception e) {
            insertError("/Staff/PageForBusiness",jsonObject.toJSONString(),getExceptionMsg(e),"api");
            return anomalous(getExceptionMsg(e));
        }
    }


    //编辑
    @RequestMapping(value = "EditGov", method = RequestMethod.POST)
    public MyResult EditGov(@RequestBody JSONObject jsonObject) {

        try {
            String timeStr = jsonObject.getString("timeStr");
            String sign = jsonObject.getString("sign");
            String staffCode = jsonObject.getString("staffCode");

            String governmentCode = jsonObject.getString("governmentCode");
            String roleCode = jsonObject.getString("roleCode");
            String realName = jsonObject.getString("realName");
            String phone = jsonObject.getString("phone");
            int status = jsonObject.getInteger("status");
            String zoneCode = jsonObject.getString("zoneCode");

            String zoneName = "";

            String staffCode2 = jsonObject.getString("staffCode2");

            PreCheck preCheck=checkSignAdmInterface(timeStr,sign,staffCode,"/Staff/EditGov");
            if(preCheck.getError()==1){
                return failed(preCheck.getMessage());
            }

            Zone zone = zoneService.getByZoneCode(zoneCode);
            if (zone != null) {
                zoneName = zone.getName();
            }


            Staff staff = staffService.getByStaffCode(staffCode2);
            if (staff == null) {
                return failed("职工不存在");
            } else {
                staff.setGovernmentCode(governmentCode);
                staff.setRealName(realName);
                staff.setPhone(phone);
                staff.setStatus(status);
                staff.setZoneCode(zoneCode);
                staff.setZoneName(zoneName);
                staff.setRoleCode(roleCode);
                staff.setParentStaffCode(staffCode);
                staffService.updateByStaffCode(staff);
            }

            Object data = new Object[]{};
            return success(data);

        } catch (Exception e) {
            insertError("/Staff/EditGov",jsonObject.toJSONString(),getExceptionMsg(e),"api");
            return anomalous(getExceptionMsg(e));
        }

    }


    //添加
    @RequestMapping(value = "InsAdd", method = RequestMethod.POST)
    public MyResult InsAdd(@RequestBody JSONObject jsonObject) {

        try {
            String timeStr = jsonObject.getString("timeStr");
            String sign = jsonObject.getString("sign");
            String staffCode = jsonObject.getString("staffCode");

            String loginName = jsonObject.getString("loginName");
            String password = jsonObject.getString("password");
            String realName = jsonObject.getString("realName");
            String phone = jsonObject.getString("phone");
            String roleCode = jsonObject.getString("roleCode");
            String zoneCode = jsonObject.getString("zoneCode");
            String zoneName = "";

            PreCheck preCheck=checkSignAdmInterface(timeStr,sign,staffCode,"/Staff/AddGov");
            if(preCheck.getError()==1){
                return failed(preCheck.getMessage());
            }

            Staff staff = staffService.getByLoginName(loginName);
            if (staff != null) {
                return failed("账户已存在");
            }

            Zone zone = zoneService.getByZoneCode(zoneCode);
            if (zone != null) {
                zoneName = zone.getName();
            }

            InstitutionStaffRel institutionStaffRel = institutionStaffRelService.getByStaffCode(staffCode);
            String insCode = institutionStaffRel.getInstitutionCode();
            Institution institution = institutionService.getByInstitutionCode(insCode);


            Staff staff3 = new Staff();
            staff3.setStaffCode(UUID.randomUUID().toString());
            staff3.setGovernmentCode(institution.getGovernmentCode());
            staff3.setInstitutionCode(insCode);
            staff3.setRoleCode(roleCode);
            staff3.setLoginName(loginName);
            staff3.setPassword(getMD5Str(password));
            staff3.setRealName(realName);
            staff3.setPhone(phone);
            staff3.setCreateTime(new Date());
            staff3.setLastLoginTime(new Date());
            staff3.setZoneCode(zoneCode);
            staff3.setZoneName(zoneName);
            staffService.insert(staff3);

            Object object = new Object[]{};
            return success(object);

        } catch (Exception e) {
            insertError("/Staff/AddGov",jsonObject.toJSONString(),getExceptionMsg(e),"api");
            return anomalous(getExceptionMsg(e));
        }

    }

    //编辑
    @RequestMapping(value = "InsEdit", method = RequestMethod.POST)
    public MyResult InsEdit(@RequestBody JSONObject jsonObject) {

        try {
            String timeStr = jsonObject.getString("timeStr");
            String sign = jsonObject.getString("sign");
            String staffCode = jsonObject.getString("staffCode");

            String roleCode = jsonObject.getString("roleCode");
            String realName = jsonObject.getString("realName");
            String phone = jsonObject.getString("phone");
            int status = jsonObject.getInteger("status");
            String zoneCode = jsonObject.getString("zoneCode");
            String zoneName = "";

            String staffCode2 = jsonObject.getString("staffCode2");

            PreCheck preCheck=checkSignAdmInterface(timeStr,sign,staffCode,"/Staff/EditGov");
            if(preCheck.getError()==1){
                return failed(preCheck.getMessage());
            }

            Zone zone = zoneService.getByZoneCode(zoneCode);
            if (zone != null) {
                zoneName = zone.getName();
            }


            InstitutionStaffRel institutionStaffRel = institutionStaffRelService.getByStaffCode(staffCode);
            String insCode = institutionStaffRel.getInstitutionCode();
            Institution institution = institutionService.getByInstitutionCode(insCode);


            Staff staff = staffService.getByStaffCode(staffCode2);
            staff.setGovernmentCode(institution.getGovernmentCode());
            staff.setInstitutionCode(insCode);
            staff.setRealName(realName);
            staff.setPhone(phone);
            staff.setStatus(status);
            staff.setZoneCode(zoneCode);
            staff.setZoneName(zoneName);
            staff.setRoleCode(roleCode);
            staff.setParentStaffCode(staffCode);
            staffService.updateByStaffCode(staff);

            Object data = new Object[]{};
            return success(data);

        } catch (Exception e) {
            insertError("/Staff/EditGov",jsonObject.toJSONString(),getExceptionMsg(e),"api");
            return anomalous(getExceptionMsg(e));
        }

    }



    //后台修改密码
    @RequestMapping(value = "EditPassword",method = RequestMethod.POST)
    public MyResult EditPassword(@RequestBody JSONObject jsonObject){
        try {
            String timeStr = jsonObject.getString("timeStr");
            String sign = jsonObject.getString("sign");
            String password = jsonObject.getString("password");
            String staffCode =jsonObject.getString("staffCode");
            String editStaffCode =jsonObject.getString("editStaffCode");
            PreCheck preCheck=checkSignAdmInterface(timeStr,sign,staffCode,"/Staff/EditPassword");
            if(preCheck.getError()==1){
                return failed(preCheck.getMessage());
            }
            Staff staff =staffService.getByStaffCode(editStaffCode);
            staff.setPassword(getMD5Str(password));
            staffService.updateByStaffCode(staff);
            JSONObject jsonObject1 = new JSONObject();
            return success(jsonObject1);
        } catch (Exception e) {
            insertError("/Staff/EditPassword",jsonObject.toJSONString(),getExceptionMsg(e),"api");
            return anomalous(getExceptionMsg(e));
        }
    }

    //职工修改自己的密码
    @RequestMapping(value = "EditMyPassword",method = RequestMethod.POST)
    public MyResult EditMyPassword(@RequestBody JSONObject jsonObject){
        try {
            String timeStr = jsonObject.getString("timeStr");
            String sign = jsonObject.getString("sign");
            String oldPassword = jsonObject.getString("oldPassword");
            String newPassword = jsonObject.getString("newPassword");
            String staffCode =jsonObject.getString("staffCode");
            PreCheck preCheck=checkSignAdmInterface(timeStr,sign,staffCode,"/Staff/EditMyPassword");
            if(preCheck.getError()==1){
                return failed(preCheck.getMessage());
            }

            Staff staff =staffService.getByStaffCode(staffCode);
            if(getMD5Str(oldPassword).equals(staff.getPassword())){
                staff.setPassword(getMD5Str(newPassword));
                staffService.updateByStaffCode(staff);
            }else {
                return failed("旧密码输入错误！");
            }
            JSONObject jsonObject1 = new JSONObject();
            return success(jsonObject1);
        } catch (Exception e) {
            insertError("/Staff/EditMyPassword",jsonObject.toJSONString(),getExceptionMsg(e),"api");
            return anomalous(getExceptionMsg(e));
        }
    }



    //职工修改自己的密码
    @RequestMapping(value = "query",method = RequestMethod.GET)
    public MyResult queryRemote(@RequestParam String realName){
        try {
            return success(staffService.query(realName));
        } catch (Exception e) {
            return anomalous(getExceptionMsg(e));
        }
    }



}
